package com.ananjt.redis;

import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RedisTest {

    private static Jedis jedis;

    public RedisTest() {
        connRedis();

    }

    public static Jedis connRedis() {
        if(jedis == null){
            synchronized (RedisTest.class){
                if(jedis == null) {
                    jedis = new Jedis("127.0.0.1", 6379);

//                    jedis .setPassword("123456");
                    //权限认证
                    jedis.auth("123456");
                }
            }
        }
        return jedis;
    }

    public void setString(){

        jedis.set("name","麻雀");
        jedis.set("name","杜鹃");

        System.out.println(jedis.get("name"));

    }

    public void setList(){

        jedis.lpush("lname","麻雀");
        jedis.lpush("lname","杜鹃");

        System.out.println(jedis.lrange("lname",0,-1));

    }

    public void setMap(){

        Map<String, String> map = new HashMap<String, String>();
        map.put("name", "xinxin");
        map.put("age", "22");
        map.put("qq", "123456");
        jedis.hmset("user",map);

        List<String> rsmap = jedis.hmget("user", "name", "age", "qq");
        System.out.println(rsmap);

    }
    public static void main(String[] args) {
        RedisTest redis = new RedisTest();
//        redis.setString();
//        redis.setList();
        redis.setMap();
    }

}
